package cn.tedu.nybikeT.controller;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.tedu.nybikeT.pojo.JsonResult;
import cn.tedu.nybikeT.service.ex.EmptyArgumentException;
import cn.tedu.nybikeT.service.ex.InsertException;
import cn.tedu.nybikeT.service.ex.ServiceException;

@ControllerAdvice
public class GlobalExceptionHandler {

	/**
	 * 统一处理业务层自定义异常的方法
	 * @param e 异常对象
	 * @return
	 */
	@ExceptionHandler(ServiceException.class)
	@ResponseBody
	public JsonResult<Void> handleServiceException(Throwable e){
		JsonResult<Void> jr=new JsonResult<Void>();
		// 封装异常提示信息
		jr.setMsg(e.getMessage());
		// 根据异常类型，设定状态码
		if(e instanceof EmptyArgumentException) {
			jr.setState(2001);
		}else if(e instanceof InsertException) {
			jr.setState(3001);
		}
		// 返回jr对象
		return jr;
	}
	
}
